<?php require 'header.php'; ?>
		<div id="main" class="container">

			<div class="section section-no-margin-bottom row entries bg-primary">
									<?php 
									$pre_id = $_GET['id'] - 1;
									
									// Check for the second last news
									$isExist = false;
									while($isExist == false){
										$check_last = $conn->query("SELECT * FROM `news` WHERE id = '".$pre_id."'");
										$very_last = $conn->query("SELECT * FROM `news` ORDER BY id ASC LIMIT 1");
										$very_last_row = $very_last->fetch();
										if($very_last_row['id']-1 == $pre_id){
											break;
										}
										if($check_last->rowCount() == 0){
											$pre_id = $pre_id - 1;
										}
										else{
											$isExist = true;
										} 
									}
								// News - ALL
								$select_news = $conn->query("SELECT * FROM `news` WHERE `id` = '".$pre_id."'");
								$row_news = $select_news->fetch(); 
								?>
				<article class="entry style-grid style-hero hero-nav type-post col-xs-6 col-sm-6">
					
					<a href="/post/<?php echo $row_news['id']; ?>">

						<header class="entry-header">
							<p class="small">Previous Story</p>
							<h3 class="entry-title"><?php echo $row_news['title']; ?></h3>
						</header>

						<figure class="entry-thumbnail">

							<div class="overlay overlay-primary"></div>
								<!-- to disable lazy loading, remove data-src and data-src-retina -->
								<img src="/author/images/<?php echo $row_news['img_name']; ?>" width="230" height="230" class="" alt="">

								<!--fallback for no javascript browsers-->
								<noscript>
									<img src="/author/images/<?php echo $row_news['img_name']; ?>" alt="">
								</noscript>

						</figure>

					</a> 

				</article>

				<?php
									$pre_id = $_GET['id'] + 1;
									// Check for the second last news 
									
									$isExist = false;
									while($isExist == false){
										$check_last = $conn->query("SELECT * FROM `news` WHERE id = '".$pre_id."'");
										$very_last = $conn->query("SELECT * FROM `news` ORDER BY id DESC LIMIT 1");
										$very_last_row = $very_last->fetch();
										if($very_last_row['id']+1 == $pre_id){
											break;
										}
										if($check_last->rowCount() == 0){
											$pre_id = $pre_id + 1;
										}
										else{
											$isExist = true;
										}
									} 

								// News - ALL
								$select_news = $conn->query("SELECT * FROM `news` WHERE `id` = '".$pre_id."'");
								$row_news = $select_news->fetch(); 
								?>
				<article class="entry style-grid style-hero hero-nav type-post col-xs-6 col-sm-6">
					
					<a href="/post/<?php echo $row_news['id']; ?>">

						<header class="entry-header">
							<p class="small">Next Story</p>
							<h3 class="entry-title"><?php echo $row_news['title']; ?></h3>
						</header>

						<figure class="entry-thumbnail">

							<div class="overlay overlay-primary"></div>
								<!-- to disable lazy loading, remove data-src and data-src-retina -->
								<img src="/author/images/<?php echo $row_news['img_name']; ?>" width="230" height="230" class="" alt="">

								<!--fallback for no javascript browsers-->
								<noscript>
									<img src="/author/images/<?php echo $row_news['img_name']; ?>" alt="">
								</noscript>

						</figure>

					</a> 

				</article>

			</div>

			<div id="content" class="content section row">

				<div class="col-md-8 bg-base col-lg-8 col-xl-9">

					<div class="ribbon ribbon-highlight">
						<ol class="breadcrumb ribbon-inner">
							<li><a href="#">Post</a></li>					</ol>
					</div>
					
					<header class="page-header">
						
					<?php 
						$select_news = $conn->prepare("SELECT * FROM `news` WHERE id = :id");
						$select_news->execute(array('id' => $_GET['id'] ));
						$row_news = $select_news->fetch();
							// Calculate Views
							$viewed = $row_news['viewed'] + 1;
							$updateviewed = $conn->prepare("UPDATE `news` SET `viewed`=$viewed WHERE id = :id");
							$updateviewed->execute(array('id' => $_GET['id'] ));	
							?>
						<div class="page-meta pull-right hidden-xs">
							<a href="#comments">
								<span class="comments-count-balloon bg-primary"><?php echo $viewed; ?><em>Views</em></span>
							</a>
						</div>
						<h2 class="page-title"><?php echo $row_news['title']; ?></h2>
					</header>
					<article class="entry style-single type-post">
							
						<?php 
							if($row_news['video'] != null){
							// YOUTUBE DESCRIPTION EXAMPLE
							$textDescription = $row_news['video'];


							// VIMEO DESCRIPTION EXAMPLE
							//$textDescription = "https://vimeo.com/45980982 The master Bob Haro was putting together a sequence for the Olympic opening that sadly got cut due to time constraints. They made this little film of some of the riders. Nice work BOB, see you at EICMA again!";


							$parsed     = parse_url($textDescription);
							$hostname   = $parsed['host'];  // WWW.YOUTUBE.COM
							$query      = $parsed['query']; // v=5sRDHnTApSw&feature=youtu.be.......to end of the string
							$path       = $parsed['path']; // this is for vimeo.com

							//print_r($parsed);

							$Arr = explode('v=',$query);
							// from video id, until to end of the string like 5sRDHnTApSw&feature=youtu.be The master Bob Haro......
							$videoIDwithString = $Arr[1];

							$videoID = substr($videoIDwithString,0,11); // 5sRDHnTApSw
							//print_r($videoID);

							//YOUTUBE.COM
							if( (isset($videoID)) && (isset($hostname)) && ($hostname=='www.youtube.com' || $hostname=='youtube.com')){

							?>
							<iframe width="640" height="360" src="https://www.youtube.com/embed/<?php echo $videoID; ?>" frameborder="0" allowfullscreen style="width: 100%; height:450px;"></iframe>
												<p><?php echo $row_news['description']; ?></</p>

							<?php 
							}
							}
							else {
						?>
					<p><?php echo $row_news['description']; ?></</p>

						<figure class="entry-thumbnail">

							<!-- to disable lazy loading, remove data-src and data-src-retina -->
							<img src="/author/images/<?php echo $row_news['img_name']; ?>" width="680" height="452" alt="">

							<!--fallback for no javascript browsers-->
							<noscript>
								<img src="/author/images/<?php echo $row_news['img_name']; ?>" alt="">
							</noscript>

						</figure>
						<?php 
							}
						?>

						<div class="entry-meta indented">
							<?php 
											$select_news_author = $conn->query("SELECT * FROM `users` WHERE id = '".$row_news['author_id']."'");
											$row_news_author = $select_news_author->fetch();
										?>
										<span class="author">by <a><?php echo $row_news_author['firstname'] . " " . $row_news_author['lastname']; ?></a></span>
										
										<span class="entry-date"><a href="#">on <time datetime="2013-07-04T23:26:34+00:00"><?php echo @date("F jS Y",strtotime($row_news['pub_date'])); ?></time></a></span>
										<?php 
											$select_news_cat = $conn->query("SELECT * FROM news_categories WHERE id = '".$row_news['category_id']."'");
											$row_news_cat = $select_news_cat->fetch();
										?>
										<span class="category">In <a href="details.php?cid=<?php echo $row_news_cat['id']; ?>"><?php echo $row_news_cat['category_name']; ?></a></span>
						</div>

						<div class="entry-content indented">
							<?php echo $row_news['content']; ?>
						</div>

					</article>

					<div class="after-entry indented">

							<!-- <hr>


					<aside class="widget post-tags">
							<i class="fa fa-tags"></i> Tags:
							<a href="#"> Some</a>
							<a href="#"> Goes</a>
							<a href="#"> Here</a>
							<a href="#"> For</a>
							<a href="#"> Sure</a>
						</aside> -->

						<hr>

						<aside class="widget widget-author media">
							<?php 
							$select_other_author_data = $conn->query("SELECT * FROM `user_authors` WHERE id = '".$row_news_author['id']."'");
							$row_other_author_data = $select_other_author_data->fetch();
							?>
							<figure class="entry-author-avatar media-object avatar pull-left">
								<!-- to disable lazy loading, remove data-src and data-src-retina -->
								<?php if($row_other_author_data['image'] == null OR $row_other_author_data['image'] == " ") {  ?>
								<img src="/assets/img/placeholder.gif" width="230" height="230" class="img-circle" alt="">

								<!--fallback for no javascript browsers-->
								<noscript>
									<img src="/assets/img/placeholder.gif" alt="">
								</noscript>
								<?php } else { ?>
								<img src="/assets/img/authors/<?php echo $row_other_author_data['image']; ?>" width="230" height="230" class="img-circle" alt="">

								<!--fallback for no javascript browsers-->
								<noscript>
									<img src="/assets/img/authors/<?php echo $row_other_author_data['image']; ?>" alt="">
								</noscript>
								<?php } ?>
							</figure>

							<div class="media-body">
								<h4 class="entry-author-name">By <a href="#"><?php echo $row_news_author['firstname'] . " " . $row_news_author['lastname']; ?></a></h4>

								<p>
									<?php echo $row_other_author_data['bio']; ?> 
								</p>

							</div>

						</aside>

						<hr>

						<aside class="widget">

							<h4 class="widget-title">Related Posts</h4>

							<div class="entries row">
								<?php 
								// News - ALL
								$select_news = $conn->query("SELECT * FROM `news` WHERE `category_id`='".$row_news['category_id']."' ORDER BY pub_date DESC LIMIT 0, 4");
								while($row_news = $select_news->fetch()){
								?>
									<article class="entry type-post style-thumbnail col-sm-3 col-md-3">

										<figure class="entry-thumbnail">
											<a href="/post/<?php echo $row_news['id']; ?>">
												<!-- to disable lazy loading, remove data-src and data-src-retina -->
											<img src="/author/images/<?php echo $row_news['img_name']; ?>" width="230" height="230" class="" alt="">

													<!--fallback for no javascript browsers-->
													<noscript>
														<img src="/author/images/<?php echo $row_news['img_name']; ?>" alt="">
													</noscript>
											</a>
										</figure>

										<h3 class="entry-title"><a href="/post/<?php echo $row_news['id']; ?>"><?php echo $row_news['title']; ?></a> </h3>
									
									</article>
									<?php } ?>
							</div>

						</aside>

						<hr>
<?php 
						$select_news = $conn->prepare("SELECT * FROM `news` WHERE id = :id");
						$select_news->execute(array('id' => $_GET['id'] ));
						$row_news = $select_news->fetch(); ?>
						<aside class="widget">
							Share:
							<a href="https://www.facebook.com/sharer/sharer.php?u=http://tech.af/post/<?php echo $_GET['id']; ?>" target="_blank" class=""><span class="fa fa-stack"><i class="fa fa-sign-blank icon-stack-base"></i><i class="fa fa-facebook-square fa-2x"></i></span></a>
							<a target="_blank" href="http://twitter.com/share?text=<?php echo $row_news['title']; ?>&url=http://tech.af/post/<?php echo $_GET['id']; ?>&hashtags=Tech,Afghanistan"><span class="fa fa-stack"><i class="fa fa-sign-blank icon-stack-base"></i><i class="fa fa-twitter-square fa-2x"></i></span></a>
							<a href="#" class="" onclick="window.open(
								'https://plus.google.com/share?url='+encodeURIComponent(location.href), 
								'facebook-share-dialog', 
								'width=500,height=436'); 
								return false;"><span class="fa fa-stack"><i class="fa fa-sign-blank icon-stack-base"></i><i class="fa fa-google-plus-square fa-2x icon-light"></i></span></a>
							<a href="#" class="" onclick="javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());"><span class="fa fa-stack"><i class="fa fa-sign-blank icon-stack-base"></i><i class="icon-light"></i></span></a>
						</aside> 

					</div>
					
					<div id="comments" class="comments-area">

						<h4 class="comments-title">Comments</h4>
						      <div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'tech-af'; // required: replace example with your forum shortname

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

					</div><!--/#comments-->

				</div><!--/.col-md-8.col-lg-8.col-xl-9-->

				<div class="sidebar col-md-4 col-lg-4 col-xl-3">

					<aside class="widget">
						<?php require 'sidebar-mostviewed.php'; ?>

					</aside><!--/.widget-->

					<aside id="reviewCarousel" class="widget carousel slide">

						<?php require 'rssbar.php'; ?>
					</aside><!--/.widget.carousel-->
					<aside class="widget">
						<?php require 'sidebar-ads.php'; ?>
					</aside><!--/.widget-->

				</div><!--/.sidebar col-md-4 col-lg-4 col-xl-3-->

			</div><!--/#content-->
			
		</div><!--#main.container-->

		<?php require 'footer.php'; ?>